﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.Blog.DTO.BlogGlobalConfig;
using AnQinFrameWorkAPI.Entity.Blog.Entity;
using AnQinFrameWorkAPI.Entity.Blog.View.BlogGlobalConfig;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.Blog;

public class BlogGlobalConfigService
{
    // 数据库上下文
    private readonly DatabaseContext _context;

    // 自动映射器
    private readonly IMapper _mapper;

    // 构造函数注入依赖
    public BlogGlobalConfigService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #region 获取全局配置列表

    public async Task<RequestResponseModel> GetBlogGlobalConfigList(BlogGlobalConfigSearchDTO searchDTO)
    {
        // 构建查询条件
        var query = _context.BlogGlobalConfig
            .ProjectTo<BlogGlobalConfigView>(_mapper.ConfigurationProvider)
            .AsNoTracking().WhereIF(a => a.Code.Contains(searchDTO.Code), !string.IsNullOrEmpty(searchDTO.Code))
            .WhereIF(a => a.Content.Contains(searchDTO.Content), !string.IsNullOrEmpty(searchDTO.Content))
            .WhereIF(a => a.CreateTime > searchDTO.StartTime, searchDTO.StartTime != null)
            .WhereIF(a => a.CreateTime < searchDTO.EndTime, searchDTO.EndTime != null)
            .WhereIF(a => a.Status == searchDTO.Status, searchDTO.Status != null);
        // 获取分页数据
        var configList = await query
            .OrderBy(a => a.CreateTime)
            .Page(searchDTO.PageIndex, searchDTO.PageSize)
            .ToListAsync();
        // 获取总数
        var pageCount = await query.CountAsync();
        // 返回分页结果
        var pageResult = new PageResult<BlogGlobalConfigView>(configList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增全局配置

    public async Task<RequestResponseModel> AddBlogGlobalConfig(BlogGlobalConfigAddDTO addDTO)
    {
        // 映射到实体模型
        var config = _mapper.Map<BlogGlobalConfigModel>(addDTO);
        // 设置默认值
        config.Id = Guid.NewGuid();
        config.CreateTime = DateTime.Now;
        // 添加到数据库
        _context.BlogGlobalConfig.Add(config);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(config.Id, "新增成功");
    }

    #endregion

    #region 更新全局配置

    public async Task<RequestResponseModel> PutBlogGlobalConfig(BlogGlobalConfigUpdateDTO updateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.BlogGlobalConfig, updateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("全局配置Id无效");
        ;
        //编辑数据
        var blogBlog = _mapper.Map(updateDTO, query);
        // 保存到数据库
        _context.BlogGlobalConfig.Update(blogBlog);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(updateDTO.Id, "更新成功");
    }

    #endregion

    #region 删除全局配置

    public async Task<RequestResponseModel> DeleteBlogGlobalConfig(SingleIdDTO deleteDTO)
    {
        // 验证ID有效性
        var config = await Valid.ValidId(_context.BlogGlobalConfig, deleteDTO.Id);
        if (config == null) return RequestResponse.ErrorRequest("全局配置ID无效");

        // 执行删除
        _context.BlogGlobalConfig.Remove(config);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(deleteDTO.Id, "删除成功");
    }

    #endregion
}